//
//  YFWeiboRefreshView.swift
//  刷新控件
//
//  Created by tsaievan on 2017/9/13.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit

class YFWeiboRefreshView: YFRefreshView {
    
    /// 袋鼠图像imageView
    @IBOutlet weak var kangarooImageView: UIImageView!
    
    
    /// 背景房屋imageView
    @IBOutlet weak var backgroundView: UIImageView!
    
    /// 地球imageView
    
    @IBOutlet weak var earthImageView: UIImageView!
    
    
    override var pullingHeight: CGFloat {
        didSet {
            
            ///< 当scale变小时, 需要设置锚点
            kangarooImageView.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
            kangarooImageView.center = CGPoint(x: self.bounds.width * 0.5, y: self.bounds.height * 0.5 + 47)
            let maxHeight: CGFloat = 120
            var scale = pullingHeight / maxHeight
            if scale > 2.0 {
                scale = 2.0
            }
            kangarooImageView.transform = CGAffineTransform(scaleX: scale, y: scale)
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.duration = 2
        animation.toValue = Double.pi * 2
        animation.repeatCount = MAXFLOAT
        earthImageView.layer.add(animation, forKey: "transform.rotation")
        
        var arrayM = [UIImage]()
        for i in 1...6 {
            let imageName = "icon_small_kangaroo_loading_\(i)"
            let image = UIImage(named: imageName) ?? UIImage()
            arrayM.append(image)
        }
        kangarooImageView.animationImages = arrayM
        kangarooImageView.animationDuration = 0.5
        kangarooImageView.startAnimating()
    }
}
